package com.ntanh.mealplanner.main.dao;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.ntanh.mealplanner.base.model.BaseEntity;
import com.ntanh.mealplanner.main.objects.Meal;
import com.ntanh.mealplanner.main.objects.MealType;
import com.ntanh.mealplanner.main.objects.Recipe;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Anh Nguyen on 2/27/2015.
 */
public class MealDao extends BaseDao {

    public MealDao(Context context) {
        super(context);
    }

    public ArrayList<Meal> getAll(String author, boolean lazy) {
        ArrayList<Meal> meals = new ArrayList<Meal>();
        RecipeDao recipeDao = new RecipeDao(mContext);
        String query = "SELECT " + "*"
                + " FROM " + DataBaseHelper.MEAL_TABLE;

        Log.d("query", query);
        Cursor cursor = database.rawQuery(query, null);
        while (cursor.moveToNext()) {
            Meal meal = convertFromCursor(cursor, author, recipeDao, lazy);
            meals.add(meal);
        }
        return meals;
    }

    public Meal getById(String author, Integer id) {
        RecipeDao recipeDao = new RecipeDao(mContext);
        Meal meal = null;
        String query = "SELECT " + "*"
                + " FROM " + DataBaseHelper.MEAL_TABLE
                + " WHERE id = " + id;
        Log.d("query", query);
        Cursor cursor = database.rawQuery(query, null);
        while (cursor.moveToNext()) {
            meal = convertFromCursor(cursor, author, recipeDao, false);
        }
        return meal;
    }

    private Meal convertFromCursor(Cursor cursor, String author, RecipeDao recipeDao, boolean lazy) {
        Meal meal = new Meal(author);
        meal.setId(cursor.getInt(0));
        try {
            meal.setCreatedDate(BaseEntity.dateFormatter.parse(cursor.getString(1)));
        } catch (ParseException e) {
            meal.setCreatedDate(null);
        }
        try {
            meal.setModifiedDate(BaseEntity.dateFormatter.parse(cursor.getString(2)));
        } catch (ParseException e) {
            meal.setModifiedDate(null);
        }
        meal.setCreatedBy(cursor.getString(3));
        meal.setModifiedBy(cursor.getString(4));
        meal.setActive(cursor.getInt(5) == 1);
        meal.setDeleted(cursor.getInt(6) == 1);
        meal.setSynced(cursor.getInt(7) == 1);
        if (!lazy) {
            String recipeStr = cursor.getString(8);
            if(recipeStr != null) {
                if (recipeStr.contains(BaseEntity.entitySplitSign)) {
                    String[] array = recipeStr.split(BaseEntity.entitySplitSign);
                    for (String recipeId : array) {
                        if (!recipeId.isEmpty()) {
                            Recipe recipe = recipeDao.getById(author, Integer.parseInt(recipeId));
                            if (recipe != null) {
                                meal.getRecipeList().add(recipe);
                            }
                        }
                    }
                } else if (!recipeStr.isEmpty()) {
                    Recipe recipe = recipeDao.getById(author, Integer.parseInt(recipeStr));
                    if (recipe != null) {
                        meal.getRecipeList().add(recipe);
                    }
                }
            }
        }
        meal.setType(MealType.getFromName(mContext,cursor.getString(9)));

        return meal;
    }
}
